package com.kreedx.kdemo.module;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.taobao.luaview.global.LuaScriptLoader;
import com.taobao.luaview.global.LuaView;
import com.taobao.luaview.scriptbundle.ScriptBundle;
import com.taobao.luaview.util.AssetUtil;
import com.taobao.luaview.util.JsonUtil;
import com.taobao.luaview.util.LogUtil;
import com.taobao.luaview.view.LVLoadingDialog;

import org.luaj.vm2.LuaValue;


public class LuaDemoActivity extends AppCompatActivity {

    private LuaView mLuaView;
    private LVLoadingDialog mDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        showLoading();
        createLuaViewAsync();
    }


    private void showLoading() {
        mDialog = new LVLoadingDialog(this);
        mDialog.show();
    }

    private void hideLoading() {
        if (mDialog != null) {
            mDialog.cancel();
        }
    }

    /**
     * 异步创建LuaView（推荐方式）
     */
    private void createLuaViewAsync() {
        LuaView.createAsync(this, new LuaView.CreatedCallback() {
            @Override
            public void onCreated(LuaView luaView) {
                hideLoading();
                mLuaView = luaView;
                if (mLuaView != null) {
                    extendsLuaView(mLuaView);
                    loadScript(mLuaView);
                    setContentView(mLuaView);
                }
            }
        });
    }

    /**
     * 同步创建LuaView
     */
    private void createLuaView(){
        mLuaView = LuaView.create(this);
        extendsLuaView(mLuaView);
        loadScript(mLuaView);
        setContentView(mLuaView);
        hideLoading();
    }

    /**
     * 扩展LuaView
     * 注册Panel（如果已经有UI组件，需要在Lua使用）
     * 注册Bridge（有部分API或功能，需要在Lua使用）
     */
    private void extendsLuaView(final LuaView luaView) {

        luaView.setUseStandardSyntax(false);//是否使用标准语法
    }

    /**
     * 加载数据
     */
    public void loadScript(final LuaView luaView) {
        luaView.load(getLuaUri(), new LuaScriptLoader.ScriptExecuteCallback() {
            @Override
            public boolean onScriptPrepared(ScriptBundle bundle) {
                return false;
            }

            @Override
            public boolean onScriptCompiled(LuaValue value, LuaValue context, LuaValue view) {
                return false;
            }

            @Override
            public void onScriptExecuted(String uri, boolean executedSuccess) {
                //测试调用 lua function
                LogUtil.d("call-lua-function return:", luaView.callLuaFunction("global_fun_test1", 1, "a", 0.1));
                LogUtil.d("call-lua-function return:", JsonUtil.toString(luaView.callLuaFunction("global_fun_test2", 2, "b", 0.2)));
                LogUtil.d("call-window-function return:", luaView.callWindowFunction("window_fun1", 3, "c", 0.3));
                LogUtil.d("call-window-function return:", luaView.callWindowFunction("window_fun2", 4, "d", 0.4));
            }
        });
    }

    /**
     * load bytecode directly
     *
     * @param luaView
     */
    public void loadBytecodeScript(final LuaView luaView) {
        luaView.loadPrototype(AssetUtil.open(this, "test/lvp/UI_Window.luap"), "UI_window", new LuaScriptLoader.ScriptExecuteCallback() {
            @Override
            public boolean onScriptPrepared(ScriptBundle bundle) {
                return false;
            }

            @Override
            public boolean onScriptCompiled(LuaValue value, LuaValue context, LuaValue view) {
                return false;
            }

            @Override
            public void onScriptExecuted(String uri, boolean executedSuccess) {
            }
        });
    }

    /**
     * 获取文件名称
     *
     * @return
     */
    private String getLuaUri() {

        return "UI_HelloLuaView.lua";
    }

    @Override
    public void onDestroy() {
        LogUtil.d("LuaView-onDestroy");
        super.onDestroy();
        if (mLuaView != null) {
            mLuaView.onDestroy();
        }
    }
}
